package testsuite_homepage.pageobject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import testsuite_homepage.FalseLoginHomepage;

public class POMLoginForm {

	private final WebDriver driver;

	private By loginformLocator = By.id("form-login");
	private By usernameLocator = By.id("modlgn_username");
	private By passwordLocator = By.id("modlgn_passwd");
	private By loginButtonLocator = By.name("Submit");

	public POMLoginForm(WebDriver driver) {
		this.driver = driver;
		if (!driver.findElement(loginformLocator).isDisplayed()) {
			throw new IllegalStateException("There is no login form");
		}
	}

	public POMLoginForm typeUsername(String username) {
		driver.findElement(usernameLocator).sendKeys(username);
		return this;

	}

	public POMLoginForm typePassword(String password) {
		driver.findElement(passwordLocator).sendKeys(password);
		return this;
	}

	public LoggedInHomepage submitLogin() {
		driver.findElement(loginButtonLocator).submit();
		return new LoggedInHomepage(driver);
	}

	public FalseLoginHomepage submitFalseLogin() {
		driver.findElement(loginButtonLocator).submit();
		return new FalseLoginHomepage(driver);
	}

	public LoggedInHomepage loginAs(String username, String password) {
		typeUsername(username);
		typePassword(password);
		return submitLogin();
	}

}
